<template>
  <el-dialog
    :model-value="visible"
    title="货代订单"
    width="600"
    :close-on-click-modal="false"
    @close="cls"
  >
    <el-form
      ref="formRel"
      :model="form"
      :rules="rules"
      label-width="120px"
      style="max-width: 800px"
    >
      <el-form-item label="货代单号">
        <el-input v-model="form.hcode" disabled />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="采购单号">
            <el-input v-model="form.pcode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="对应客户订单号">
            <el-input v-model="form.code" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="货代名称">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购费用">
            <el-input v-model="form.price" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="始发地">
            <el-input v-model="form.start" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="目的地">
            <el-input v-model="form.end" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="抵达日期">
        <el-date-picker
          v-model="form.endtime"
          type="date"
          format="YYYY-MM-DD"
          placeholder="选择日期"
        />
      </el-form-item>

      <div>
        <el-button type="primary" @click="savefrom">保存</el-button>
        <el-button type="default" @click="cls">取消</el-button>
      </div>

      <!-- Add other fields similarly -->
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage, dayjs } from "element-plus";
import {
  gethcode,
  hsave,
  pUpdateStatus,
  pquery,
  query,
  updateStatus
} from "@/api/order.js";

const form = ref({
  pcode: "",
  code: "",
  hcode: "",
  name: "",
  product: "",
  start: "",
  end: "",
  endtime: "",
  status: ""
});
//更新状态用
const order = ref({
  code: "",
  status: ""
});

async function savefrom() {
  let endtimeformat = dayjs(form.value.endtime).format("YYYY-MM-DD");
  form.value.endtime = endtimeformat;
  form.value.status = "3";
  let res = await hsave(form.value);
  if (res.code == 200) {
    await pUpdateStatus(order.value);
    let res3 = await updateStatus(order.value);
    if (res3.code == 200) {
      ElMessage({
        message: "保存成功",
        type: "success"
      });
      emits("refresh");
      cls();
    }

    //刷新部门列表
    // emits("refresh");
    // cls();
  } else {
  }
}
const emits = defineEmits(["close", "refresh"]);

function cls() {
  emits("close");
}
async function init(code, status) {
  let hcodeget = await gethcode();
  let a = {
    code: code
  };
  let pcodeget = await pquery(a);
  let endget = await query(a);
  form.value.hcode = hcodeget.data;
  form.value.code = code;
  form.value.pcode = pcodeget.data[0].pcode;
  form.value.end = endget.data[0].end;
  form.value.endtime = endget.data[0].endtime;
  order.value.code = code;
  order.value.status = status;
}
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
});

defineExpose({ init });
</script>
